package com.springapp.mvc.dao;

import com.springapp.mvc.entity.CommentForPost;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * Created by dbychkov on 26.12.2014.
 */
@Component
public class CommentsDaoImpl implements CommentsDao{

    @Autowired
    private JdbcTemplate template;

    public void createComment(Long postId, Long userId, String comment){
        String q = "INSERT INTO comments(post_id, user_id, comment_text) VALUES (?, ?, ?)";
        template.update(q, postId, userId, comment);
    }

    public List<CommentForPost> getCommentsForPost(Long postId){
        String q = "SELECT " +
                "c.id comment_id, " +
                "u.id comment_user_id, " +
                "p.id post_id, " +
                "c.date_created comment_date_created, " +
                "u.name user_name, " +
                "c.comment_text comment_text " +
                "FROM comments c " +
                   "INNER JOIN posts p ON c.post_id = p.id " +
                   "INNER JOIN users u ON c.user_id = u.id " +
                   "WHERE p.id = ?";
        return template.query(q, Mappers.COMMENTS_FOR_POST_MAPPER, postId);
    }
}
